package com.quartet.api;

import com.quartet.utils.LocaleUtils;
import com.quartet.utils.constant.MapServerConstant;
import lombok.Data;

import java.io.Serializable;
import java.util.Locale;

/**
 * <p>
 * 统一接口返回值
 * </p>
 *
 * @author yuyanan
 * @since 2019/5/22
 */
@Data
public class ApiResult<T> implements Serializable {

    /**
     * 返回码
     */
    private String code;

    /**
     * 描述
     */
    private String msg;

    /**
     * 数据
     */
    private T data;

    public ApiResult(String code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public ApiResult(String code, String msg, T t) {
        this.code = code;
        this.msg = msg;
        data = t;
    }

    public static <T> ApiResult<T> ok() {
        return ok(null);
    }


    public static <T> ApiResult<T> failed(String errorCode, String msg, T data) {
        return new ApiResult(errorCode, msg, data);
    }

    public static <T> ApiResult<T> ok(T data) {
        if (LocaleUtils.getLocale().startsWith(MapServerConstant.EN)) {
            return new ApiResult("000000", "do success", data);
        } else {
            return new ApiResult("000000", "执行成功", data);
        }
    }

}
